package com.beauty.dressing.logic.db;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.beauty.dressing.common.bean.ShopCarBean;
import com.beauty.dressing.common.bean.User;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;

/**
 * SHOP_CAR 表的相关创建与绑定
 *
 * @author xutingz
 */
public class DbShopCar extends AbstractDao<ShopCarBean, Long> {

    public static final String TABLENAME = "SHOP_CAR";

    /**
     * Properties of entity LoginUser.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property _Id = new Property(0, Long.class, "_id", true, "_ID");
        public final static Property Id = new Property(1, Long.class, "id", false, "ID");
        public final static Property Name = new Property(2, String.class, "name", false, "NAME");
        public final static Property ImgUrl = new Property(3, String.class, "imgUrl", false, "IMGURL");
        public final static Property Price = new Property(4, Float.class, "price", false, "PRICE");
        public final static Property Deposit = new Property(5, Float.class, "deposit", false, "DEPOSIT");
        public final static Property Count = new Property(6, Integer.class, "count", false, "COUNT");
        public final static Property Type = new Property(7, Integer.class, "type", false, "TYPE");
        public final static Property ShopCar = new Property(8, Long.class, "insertTime", false, "INSERTTIME");
    }

    public DbShopCar(DaoConfig config) {
        super(config);
    }

    public DbShopCar(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
    }

    /**
     * Creates the underlying database table.
     */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists ? "IF NOT EXISTS " : "";
        db.execSQL("CREATE TABLE " + constraint + "\"SHOP_CAR\" (" + //
                "\"_ID\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: _id
                "\"ID\" REAL," +                                   // 1: id
                "\"NAME\" TEXT," +                           // 名称
                "\"IMGURL\" TEXT," +                            // 图片
                "\"PRICE\" REAL," +                            // 单价
                "\"DEPOSIT\" REAL," +                          // 租金
                "\"COUNT\" INTEGER," +                        //  个数
                "\"TYPE\" INTEGER," +                        //  类型
                "\"INSERTTIME\" REAL);");                         //:插入时间
    }

    /**
     * Drops the underlying database table.
     */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"SHOP_CAR\"";
        db.execSQL(sql);
    }


    /**
     * @inheritdoc
     */
    @Override
    protected void bindValues(SQLiteStatement stmt, ShopCarBean entity) {
        stmt.clearBindings();
        long _id = entity._id;
        if (_id != 0) {
            stmt.bindLong(1, _id);
        }
        long id = entity.id;
        if (id != 0) {
            stmt.bindLong(2, id);
        }
        String name = entity.name;
        if (name != null) {
            stmt.bindString(3, name);
        }
        String imgUrl = entity.imgUrl;
        if (imgUrl != null) {
            stmt.bindString(4, imgUrl);
        }
        float price = entity.price;
        if (price != 0) {
            stmt.bindDouble(5, price);
        }
        float deposit = entity.deposit;
        if (deposit != 0) {
            stmt.bindDouble(6, deposit);
        }
        int count = entity.count;
        if (count != 0) {
            stmt.bindLong(7, count);
        }
        int type = entity.type;
        if (type != 0) {
            stmt.bindLong(8, type);
        }
        long insertTime = entity.insertTime;
        if (insertTime != 0) {
            stmt.bindLong(9, insertTime);
        }
    }

    /**
     * @inheritdoc
     */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }

    /**
     * @inheritdoc
     */
    @Override
    public ShopCarBean readEntity(Cursor cursor, int offset) {
        ShopCarBean entity = new ShopCarBean(
                cursor.isNull(offset + 0) ? 0 : cursor.getLong(offset + 0),       // _id
                cursor.isNull(offset + 1) ? 0 : cursor.getLong(offset + 1),       // id
                cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // name
                cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // imgurl
                cursor.isNull(offset + 4) ? 0 : cursor.getFloat(offset + 4), // price
                cursor.isNull(offset + 5) ? 0 : cursor.getFloat(offset + 5), // deposit
                cursor.isNull(offset + 6) ? 0 : cursor.getInt(offset + 6),  // count
                cursor.isNull(offset + 7) ? 0 : cursor.getInt(offset + 7),  // type
                cursor.isNull(offset + 8) ? 0 : cursor.getLong(offset + 8)  // type
        );
        return entity;
    }

    /**
     * @inheritdoc
     */
    @Override
    public void readEntity(Cursor cursor, ShopCarBean entity, int offset) {
        entity._id = cursor.isNull(offset + 0) ? 0 : cursor.getLong(offset + 0);
        entity.id = cursor.isNull(offset + 1) ? 0 : cursor.getLong(offset + 1);
        entity.name = cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2);
        entity.imgUrl = cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3);
        entity.price = cursor.isNull(offset + 4) ? 0 : cursor.getFloat(offset + 4);
        entity.deposit = cursor.isNull(offset + 5) ? 0 : cursor.getFloat(offset + 5);
        entity.count = cursor.isNull(offset + 6) ? 0 : cursor.getInt(offset + 6);
        entity.type = cursor.isNull(offset + 7) ? 0 : cursor.getInt(offset + 7);
        entity.insertTime = cursor.isNull(offset + 8) ? 0 : cursor.getLong(offset + 8);

    }

    /**
     * @inheritdoc
     */
    @Override
    protected Long updateKeyAfterInsert(ShopCarBean entity, long rowId) {
        entity._id = rowId;
        return rowId;
    }

    /**
     * @inheritdoc
     */
    @Override
    public Long getKey(ShopCarBean entity) {
        if (entity != null) {
            return entity._id;
        } else {
            return null;
        }
    }

    /**
     * @inheritdoc
     */
    @Override
    protected boolean isEntityUpdateable() {
        return true;
    }

}
